<?php

!defined('IN_HDWIKI') && exit('Access Denied');

class control extends base{

	function control(& $get,& $post){
		$this->base( & $get,& $post);
		$this->load('attachment');
		$this->load('user');
	}

 	function douploadimg() {
 		if($this->post['picWidthHeight']==1){
			$imgwidth = 300;
			$imgheigth = 300;
 		}else{
			$imgwidth = 140;
			$imgheigth = 140;
 		}
 		$imgname=$_FILES['photofile']['name'];
		$extname=file::extname($imgname);
		$destfile=$_ENV['attachment']->makepath($extname);
	    $arrupload=file::uploadfile($_FILES['photofile'],$destfile);

		if($arrupload['result']==true){
			if($this->user['uid']<1){
				$useruid = 0;
			}else{
				$useruid = $this->user['uid'];
			}
			if(!$this->post['docid']){
				$did = 0;
			}else{
				$did = $this->post['docid'];
			}
			$_ENV['attachment']->add_attachment($useruid ,$did,$imgname ,$destfile ,$this->post['picAlt'] ,$extname);
			$arrimg=util::image_compress($destfile,'s_',$imgwidth,$imgheigth);
			$_ENV['user']->add_credit($useruid,'attachment-upload',$this->setting['credit_upload']);
		}else {
			 $this->showmsg($arrupload['msg']);
			 exit;
		}
		if($arrimg['result']==false){
			$this->showmsg($this->view->lang['filetypError']);
			exit;
		}
		else{
			$file_url=$arrimg['tempurl'];
		}
		if (empty($_FILES) === false) {
	 		echo '<html>';
			echo '<head>';
			echo '<title>Insert Image</title>';
			echo "<meta http-equiv='content-type' content='text/html; charset=".WIKI_CHARSET."'>";
			echo '</head>';
			echo '<body>';
			echo '<script type="text/javascript">parent.ThcInsertPicOrder("'.$file_url.'","'.$this->post['picWidthHeight'].'","'.$this->post['picAlign'].'","'.$this->post['picBorder'].'","'.$this->post['picHspace'].'","'.$this->post['picVspace'].'","'.$this->post['picAlt'].'","'.$this->post['picMod'].'","'.$this->post['picText'].'");</script>';
			echo '</body>';
			echo '</html>';
		}
 	}

	function showmsg($msg){
		echo '<html>';
		echo '<head>';
		echo '<title>error</title>';
		echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">";
		echo '</head>';
		echo '<body>';
		echo '<input id = "msg" type="hidden" value ="'.$msg.'">';
		echo '<script type="text/javascript">alert(document.getElementById("msg").value);parent.ThcViewNone();</script>';
		echo '</body>';
		echo '</html>';
	}
	
	function dodownload(){
		if(@!is_numeric($this->get[2])){
			$this->message($this->view->lang['parameterError'],'BACK');
		}
		$result=$_ENV['attachment']->get_attachment('id',$this->get[2]);
		if(!(bool)$attachment=$result[0]){
			$this->message($this->view->lang['attachIsNotExist'],'BACK');
		}
		$_ENV['attachment']->update_downloads($attachment['id']);
		file::downloadfile($attachment['attachment'],$attachment['filename']);
	}
}
?>